/*=======================================================================
Creator: Jingyuan Wang, jingyuanwang@u.northwestern.edu
Date created: 				08/12/2018
Date last modified: 		
Purpose: 

1. Clean eGRID electricity intensity  (state-year)


==========================================================================*/


*************************************************************************
* 				PART A. Import and clean eGRID				 			*
*************************************************************************
clear all

* 1. import eGRID carbon emission intensities of electricity

* (1) import state level
import excel "$EIraw/eGRID/egrid2016_data.xlsx", clear sheet("ST16") firstr

keep Stateabbreviation FIPSStatecode ///
		StateannualNOxnonbaseloadou StateozoneseasonNOxnonbasel ///
		StateannualSO2nonbaseloadou StateannualCO2nonbaseloado ///
		StateannualCH4nonbaseloadou StateannualN2Ononbaseloadou StateannualHgnonbaseloadou

rename StateannualNOxnonbaseloadou EI_NOx
rename StateozoneseasonNOxnonbasel EI_NOx_ozone
rename StateannualSO2nonbaseloadou EI_SO2
rename StateannualCO2nonbaseloado  EI_CO2
rename StateannualCH4nonbaseloadou EI_CH4
rename StateannualN2Ononbaseloadou EI_N2O
rename StateannualHgnonbaseloadou  EI_Hg

rename Stateabbreviation state
rename FIPSStatecode state_code
label var state_code "FIPS state code"

drop if _n == 1

tempfile ei
save `ei', replace

* (2) import US level
import excel "$EIraw/eGRID/egrid2016_data.xlsx", clear sheet("US16") firstr

keep USannualNOxnonbaseloadout USozoneseasonNOxnonbaselo ///
	 USannualSO2nonbaseloadout USannualCO2nonbaseloadout ///
	 USannualCH4nonbaseloadout USannualN2Ononbaseloadout ///
	 USannualHgnonbaseloadoutp

rename USannualNOxnonbaseloadout EI_NOx
rename USozoneseasonNOxnonbaselo EI_NOx_ozone
rename USannualSO2nonbaseloadout EI_SO2 
rename USannualCO2nonbaseloadout EI_CO2
rename USannualCH4nonbaseloadout EI_CH4
rename USannualN2Ononbaseloadout EI_N2O
rename USannualHgnonbaseloadoutp EI_Hg

drop if _n == 1

gen state = "US"
gen state_code = "0"

append using `ei'


* 2. clean
order state state_code
foreach v of varlist state_code-EI_Hg  {
	destring `v', replace force
}
*

* 3. merge and save
sort state_code
save "$EI/eGRID_2016.dta", replace
